﻿namespace SafetyRecordData.Data.DAL
{
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.IO;
    using System.Web;
    using Ths.Components.Common;
    using Ths.Components.Excel;
    using Wuqi.Webdiyer;

    [Serializable]
    public class DAHurtDistress : DBBase
    {
        private const string HurtDistress_DefaultList_DCOUNT = "select count(*)  FROM HurtDistress {0}";
        private const string HurtDistress_DefaultList_DSQL = "\r\n\t\tSELECT * FROM (\r\n\t\t    SELECT row_number() OVER({0}) as ROW_NUMBER,* FROM \r\n\t\t    ( \r\n\t\t        SELECT  [AppID] AS [AppID],  [Company] AS [Company],  [Address] AS [Address],  [Type] AS [Type],  [Category] AS [Category],  [HappenTime] AS [HappenTime],  [Workernumber] AS [Workernumber],  [Name] AS [Name],  [Age] AS [Age],  [Sex] AS [Sex],  [Workdate] AS [Workdate],  [Educationnal] AS [Educationnal],  [Worktype] AS [Worktype],  [Hurtbody] AS [Hurtbody],  [Leval] AS [Leval],  [Reason] AS [Reason],  [Checkcompany] AS [Checkcompany],  [Code] AS [Code],  [AddTime] AS [AddTime],  [Addperson] AS [Addperson],  [id] AS [id],  [HappenTimeDetail] AS [HappenTimeDetail]  FROM [HurtDistress]\r\n\t\t        {1}\r\n\t\t    ) as a\r\n\t\t) as b WHERE ROW_NUMBER > {2} AND ROW_NUMBER <= {3}";

        public DAHurtDistress()
        {
            base.TableName = "HurtDistress";
            base.PKField = "AppID";
        }

        public DAHurtDistress(string connectionStringName) : base(connectionStringName)
        {
            base.TableName = "HurtDistress";
            base.PKField = "AppID";
        }

        public DataTable GetBasicData(string category)
        {
            string strSql = "select * from qp_hr_SysData WHERE (type = '" + category + "')";
            return base.DBFactory.GetDataTable(strSql);
        }

        public string GetBasicDataName(string code, string type)
        {
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@id", code), new SqlParameter("@type", type) };
            string strSql = "select name from qp_hr_SysData WHERE type =@type and id=@id";
            DataTable dt = base.DBFactory.GetDataTable(strSql, CommandType.Text, param);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            return "";
        }

        public string GetDetailDepartMent(string bumenid)
        {
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@dwbh", bumenid) };
            return base.DBFactory.GetDataTable("dbo.r_get_juniors ",CommandType.StoredProcedure,param).Rows[0][1].ToString();
        }

        public DataTable GetHurtListCount(DbParameter[] parameters, string orderBy, string where)
        {
            DataTable dt = base.DBFactory.GetDataTable("proc_Hurt", CommandType.StoredProcedure, parameters);
            ExcelEntity _excel = new ExcelEntity();
            _excel.Sql = string.Format(" exec proc_HurtExcel " + where, new object[] { orderBy, where, 0, 100 });
            _excel.Parameters = parameters;
            HttpContext.Current.Session[Path.GetFileName(HttpContext.Current.Request["Url"].ToString())] = _excel;
            return dt;
        }

        public DataTable GetHurtListDetailCount(DbParameter[] parameters, string orderBy, string where)
        {
            DataTable dt = base.DBFactory.GetDataTable("proc_HurtDetail", CommandType.StoredProcedure, parameters);
            ExcelEntity _excel = new ExcelEntity();
            _excel.Sql = string.Format(" exec proc_HurtDetailExcel " + where, new object[] { orderBy, where, 0, 100 });
            _excel.Parameters = parameters;
            HttpContext.Current.Session[Path.GetFileName(HttpContext.Current.Request["Url"].ToString())] = _excel;
            return dt;
        }

        public int GetPermissionCount(string username)
        {
            string strSql = "select count(1) from qp_hr_username where  charindex(',23,',bmqx)>0 and username='" + username + "'";
            return Convert.ToInt32(base.DBFactory.ExecuteScalar(strSql));
        }

        public string GetTopDepartMentID(string bumenid)
        {
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@ID", bumenid) };
            DataTable dt = base.DBFactory.GetDataTable("dbo.proc_GetTopDepartMentID", CommandType.StoredProcedure, param);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            return "";
        }

        public DataTable HurtDistress_DefaultListPR(AspNetPager AspNetPager1, DbParameter[] parameters, string orderBy, string where)
        {
            string sql = string.Format("select count(*)  FROM HurtDistress {0}", where);
            CommonClass.InitAspPageInfo(CommonUtility.Get_Int32(base.DBFactory.ExecuteScalar(sql, CommandType.Text, parameters)), AspNetPager1);
            sql = string.Format("\r\n\t\tSELECT * FROM (\r\n\t\t    SELECT row_number() OVER({0}) as ROW_NUMBER,* FROM \r\n\t\t    ( \r\n\t\t        SELECT  [AppID] AS [AppID],  [Company] AS [Company],  [Address] AS [Address],  [Type] AS [Type],  [Category] AS [Category],  [HappenTime] AS [HappenTime],  [Workernumber] AS [Workernumber],  [Name] AS [Name],  [Age] AS [Age],  [Sex] AS [Sex],  [Workdate] AS [Workdate],  [Educationnal] AS [Educationnal],  [Worktype] AS [Worktype],  [Hurtbody] AS [Hurtbody],  [Leval] AS [Leval],  [Reason] AS [Reason],  [Checkcompany] AS [Checkcompany],  [Code] AS [Code],  [AddTime] AS [AddTime],  [Addperson] AS [Addperson],  [id] AS [id],  [HappenTimeDetail] AS [HappenTimeDetail]  FROM [HurtDistress]\r\n\t\t        {1}\r\n\t\t    ) as a\r\n\t\t) as b WHERE ROW_NUMBER > {2} AND ROW_NUMBER <= {3}", new object[] { orderBy, where, (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize });
            ExcelEntity _excel = new ExcelEntity();
            _excel.Sql = string.Format("\r\n\t\tSELECT * FROM (\r\n\t\t    SELECT row_number() OVER({0}) as ROW_NUMBER,* FROM \r\n\t\t    ( \r\n\t\t        SELECT  [AppID] AS [AppID],  [Company] AS [Company],  [Address] AS [Address],  [Type] AS [Type],  [Category] AS [Category],  [HappenTime] AS [HappenTime],  [Workernumber] AS [Workernumber],  [Name] AS [Name],  [Age] AS [Age],  [Sex] AS [Sex],  [Workdate] AS [Workdate],  [Educationnal] AS [Educationnal],  [Worktype] AS [Worktype],  [Hurtbody] AS [Hurtbody],  [Leval] AS [Leval],  [Reason] AS [Reason],  [Checkcompany] AS [Checkcompany],  [Code] AS [Code],  [AddTime] AS [AddTime],  [Addperson] AS [Addperson],  [id] AS [id],  [HappenTimeDetail] AS [HappenTimeDetail]  FROM [HurtDistress]\r\n\t\t        {1}\r\n\t\t    ) as a\r\n\t\t) as b WHERE ROW_NUMBER > {2} AND ROW_NUMBER <= {3}", new object[] { orderBy, where, 0, AspNetPager1.RecordCount });
            _excel.Parameters = parameters;
            HttpContext.Current.Session[Path.GetFileName(HttpContext.Current.Request["Url"].ToString())] = _excel;
            DataTable dt = base.DBFactory.GetDataTable(sql, CommandType.Text, parameters);
            return base.FullRow(dt, AspNetPager1.PageSize);
        }
    }
}

